package com.woniu.adapter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.woniu.exception.ReportedForWorkException;
import com.woniu.outlet.client.WorkingProcedures;
import com.woniu.outlet.dao.ApplicationDetailMapper;
import com.woniu.outlet.po.ApplicationDetail;
import com.woniu.outlet.po.ApplicationDetailExample;
import com.woniu.outlet.po.MaterialApplication;
import com.woniu.outlet.redis.ERPRedisRepo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Component
public class ApplicationAdapter {
    @Autowired
    private ERPRedisRepo erpRedisRepo;
    @Autowired
    private ApplicationDetailMapper applicationDetailMapper;
    @Autowired
    private WorkingProcedures workingProcedures;

    ObjectMapper mapper = new ObjectMapper();
    CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, MaterialApplication.class);
    /** 获取申领详情列表
     * @return
     */
    public List<ApplicationDetail> getApplicationDetailList() throws JsonProcessingException {
        List<ApplicationDetail> list = new ArrayList<>();
        String str = erpRedisRepo.getList("applicationDetailList");
        if(StringUtils.isEmpty(str)){
            list = applicationDetailMapper.selectByExample(null);
            erpRedisRepo.addList(mapper.writeValueAsString(list),"applicationDetailList");
        }else{
            list = mapper.readValue(str,type);
        }
        workingProcedures.addSystemLog("查看申领详情列表");
        return list;
    }

//    /** 根据id获取申领对象
//     * @param id
//     * @return
//     */
//    public ApplicationDetail getApplicationDetail(Integer id){
//        ApplicationDetail applicationDetail = new ApplicationDetail(id);
//        ApplicationDetail redis = erpRedisRepo.getRedis(applicationDetail);
//        if(StringUtils.isEmpty(redis)){
//            redis = applicationDetailMapper.selectByPrimaryKey(id);
//            erpRedisRepo.addRedis(redis);
//        }
//        return redis;
//    }

    /** 添加申领物料详情
     * @param applicationDetail
     */
    public void addApplicationDetail(ApplicationDetail applicationDetail){
        erpRedisRepo.deleteList("applicationDetailList");
        applicationDetailMapper.insertSelective(applicationDetail);
        workingProcedures.addSystemLog("添加申领物料详情");
    }

    /** 根据id删除物料申领详情
     * @param id
     */
    public void deleteApplicationDetail(Integer id){
        erpRedisRepo.deleteList("applicationDetailList");
        ApplicationDetail applicationDetail = applicationDetailMapper.selectByPrimaryKey(id);
        if(applicationDetail.getSpare1().equals("未领取")){
            applicationDetailMapper.deleteByPrimaryKey(id);
            workingProcedures.addSystemLog("删除id为"+id+"物料申领详情");
        }else{
            throw new ReportedForWorkException("该请领以生效,无法删除");
        }
    }

    /** 修改物料申领详情
     * @param applicationDetail
     */
    public void updateApplicationDetail(ApplicationDetail applicationDetail){
        erpRedisRepo.deleteList("applicationDetailList");
        applicationDetailMapper.updateByPrimaryKeySelective(applicationDetail);
        workingProcedures.addSystemLog("修改id为"+applicationDetail.getId()+"物料申领详情");
    }

    /** 根据清领id查询请领详情集合
     * @param matId
     * @return
     */
    public List<ApplicationDetail> getApplicationDetailByMatId(Integer matId){
//        MaterialApplication materialApplication = new MaterialApplication(matId);
//        MaterialApplication redis = erpRedisRepo.getRedis(materialApplication);
        List<ApplicationDetail> applicationDetails = null;
//        if(StringUtils.isEmpty(redis)){
//            redis = new MaterialApplication();
            ApplicationDetailExample applicationDetailExample = new ApplicationDetailExample();
            ApplicationDetailExample.Criteria criteria = applicationDetailExample.createCriteria();
            criteria.andApplicationIdEqualTo(matId);
            applicationDetails = applicationDetailMapper.selectByExample(applicationDetailExample);
//            redis.setApplicationDetails(applicationDetails);
//            erpRedisRepo.addMany(materialApplication,redis);
//        }else{
//            applicationDetails = redis.getApplicationDetails();
//        }
        workingProcedures.addSystemLog("根据清领id "+matId+"查询请领详情集合");
        return applicationDetails;
    }

}
